﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 
    /// </summary>
    public class PeerReviewBeenVoterCls
    {
        /// <summary>
        /// 得到所有的数据
        /// </summary>
        /// <returns></returns>
        public DataSet GetAll()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FBeenVoterClsCd,FPeerReviewID,FBeenVoterClsName,FDispSeq ");
            strSql.Append(" FROM dbo.t_PeerReviewBeenVoterCls order by FDispSeq");

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="beenVoterClsCd">编号</param>
        /// <param name="peerReviewID">主题</param>
        /// <returns>是否存在该记录</returns>
        public bool Exists(string beenVoterClsCd, decimal peerReviewID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_PeerReviewBeenVoterCls");
            strSql.Append(" where FBeenVoterClsCd=@FBeenVoterClsCd and FPeerReviewID=@FPeerReviewID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FBeenVoterClsCd", SqlDbType.NVarChar,30),
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13)			};
            parameters[0].Value = beenVoterClsCd;
            parameters[1].Value = peerReviewID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>是否更新</returns>
        public bool Add(ElingOA6_Admin_DSJCJ.Model.PeerReviewBeenVoterCls model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_PeerReviewBeenVoterCls(");
            strSql.Append("FBeenVoterClsCd,FPeerReviewID,FBeenVoterClsName,FDispSeq)");
            strSql.Append(" values (");
            strSql.Append("@FBeenVoterClsCd,@FPeerReviewID,@FBeenVoterClsName,@FDispSeq)");
            SqlParameter[] parameters = {
					new SqlParameter("@FBeenVoterClsCd", SqlDbType.NVarChar,30),
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13),
					new SqlParameter("@FBeenVoterClsName", SqlDbType.NVarChar,30),
					new SqlParameter("@FDispSeq", SqlDbType.Int,4)};
            parameters[0].Value = model.FBeenVoterClsCd;
            parameters[1].Value = model.FPeerReviewID;
            parameters[2].Value = model.FBeenVoterClsName;
            parameters[3].Value = model.FDispSeq;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>是否更新</returns>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.PeerReviewBeenVoterCls model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_PeerReviewBeenVoterCls set ");
            strSql.Append("FBeenVoterClsName=@FBeenVoterClsName,");
            strSql.Append("FDispSeq=@FDispSeq");
            strSql.Append(" where FBeenVoterClsCd=@FBeenVoterClsCd and FPeerReviewID=@FPeerReviewID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FBeenVoterClsName", SqlDbType.NVarChar,30),
					new SqlParameter("@FDispSeq", SqlDbType.Int,4),
					new SqlParameter("@FBeenVoterClsCd", SqlDbType.NVarChar,30),
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13)};
            parameters[0].Value = model.FBeenVoterClsName;
            parameters[1].Value = model.FDispSeq;
            parameters[2].Value = model.FBeenVoterClsCd;
            parameters[3].Value = model.FPeerReviewID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="beenVoterClsCd">被评选人编号</param>
        /// <param name="peerReviewID">主题ID</param>
        /// <returns>得到一个对象实体</returns>
        public ElingOA6_Admin_DSJCJ.Model.PeerReviewBeenVoterCls GetModel(string beenVoterClsCd, decimal peerReviewID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FBeenVoterClsCd,FPeerReviewID,FBeenVoterClsName,FDispSeq from dbo.t_PeerReviewBeenVoterCls ");
            strSql.Append(" where FBeenVoterClsCd=@FBeenVoterClsCd and FPeerReviewID=@FPeerReviewID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FBeenVoterClsCd", SqlDbType.NVarChar,30),
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13)			};
            parameters[0].Value = beenVoterClsCd;
            parameters[1].Value = peerReviewID;

            ElingOA6_Admin_DSJCJ.Model.PeerReviewBeenVoterCls model = new ElingOA6_Admin_DSJCJ.Model.PeerReviewBeenVoterCls();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 批量删除数据 事务模式
        /// <creator>chenly</creator>
        /// <date>2013-7-11</date>
        /// </summary>
        /// <param name="adNoArray">编号数组</param>
        public void DelMany(string beenVoterClsCd, decimal peerReviewID)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_PeerReviewBeenVoterCls ");
            strSql.Append("  where FBeenVoterClsCd=@FBeenVoterClsCd and FPeerReviewID=@FPeerReviewID ");

            string strSqlAd = strSql.ToString();

            cmds = new CommandInfo();

            SqlParameter[] parameters = {
					    new SqlParameter("@FBeenVoterClsCd", SqlDbType.NVarChar,30),
					    new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13)
                        };

            parameters[0].Value = beenVoterClsCd;
            parameters[1].Value = peerReviewID;

            cmds.CommandText = strSqlAd;
            cmds.Parameters = parameters;
            cmdList.Add(cmds);


            DbHelperSQL.ExecuteSqlTranWithString(cmdList);

        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="row">DataRow</param>
        /// <returns>得到一个对象实体</returns>
        public ElingOA6_Admin_DSJCJ.Model.PeerReviewBeenVoterCls DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.PeerReviewBeenVoterCls model = new ElingOA6_Admin_DSJCJ.Model.PeerReviewBeenVoterCls();
            if (row != null)
            {
                if (row["FBeenVoterClsCd"] != null)
                {
                    model.FBeenVoterClsCd = row["FBeenVoterClsCd"].ToString();
                }
                if (row["FPeerReviewID"] != null && row["FPeerReviewID"].ToString() != "")
                {
                    model.FPeerReviewID = decimal.Parse(row["FPeerReviewID"].ToString());
                }
                if (row["FBeenVoterClsName"] != null)
                {
                    model.FBeenVoterClsName = row["FBeenVoterClsName"].ToString();
                }
                if (row["FDispSeq"] != null && row["FDispSeq"].ToString() != "")
                {
                    model.FDispSeq = int.Parse(row["FDispSeq"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="beenVoterClsCd">被评选人类别编号</param>
        /// <param name="peerReviewID">主题编号</param>
        /// <returns>是否删除</returns>
        public bool Delete(string beenVoterClsCd, decimal peerReviewID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_PeerReviewBeenVoterCls ");
            strSql.Append(" where FBeenVoterClsCd=@FBeenVoterClsCd and FPeerReviewID=@FPeerReviewID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FBeenVoterClsCd", SqlDbType.NVarChar,30),
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13)			};
            parameters[0].Value = beenVoterClsCd;
            parameters[1].Value = peerReviewID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
